using System;

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using System.Configuration;

using System.Xml;

using PickGold;
using PickGold.Database;
using PickGold.Entities.Table;

namespace PickGold.Base
{
    public class CSettings : CBaseSettingInfo, ISettings
    {
        //
        #region Const
        //
        public static long DEBUG_LAST = 33L;
        //
        #endregion
        //
        public CSettings() : base() { }
        public CSettings(CDatabase db) : base(db) { }
        public CSettings(string alias) : base(alias) { }
		public CSettings(CDatabase db, string alias) : base(db, alias) { }
        //
        public string this[long id]
        {
			set
			{
				XmlNode xne = GetNode(id);
				if (xne == null)
					return;

				xne.Attributes[ITEM_VALUE].Value = value;
				this.SetValue(ITEM_VALUE, value, CParser.ConvertTo<long>(xne.Attributes[ITEM_ID].Value, -1L));
			}
            get
            {
				XmlNode xne = GetNode(id);
				if (xne == null)
					return null;

				return xne.Attributes[ITEM_VALUE].Value;
			}
        }
        public string this[string path]
        {
			set
			{
				XmlNode xne = GetNode(path);
				if (xne == null)
					return;

				xne.Attributes[ITEM_VALUE].Value = value;
				this.SetValue(ITEM_VALUE, value, CParser.ConvertTo<long>(xne.Attributes[ITEM_ID].Value, -1L));
			}
            get
            {
				XmlNode xne = GetNode(path);
				if (xne == null)
					return null;

				return xne.Attributes[ITEM_VALUE].Value;
            }
        }
        //
        public XmlNode GetNode(long id)
        {
            return this.GetXmlTree().SelectSingleNode(string.Format("//*[@{0}={1}]", ITEM_ID, id));
        }
        public XmlNode GetNode(string path)
        {
			if (string.IsNullOrEmpty(path))
                return null;

			if (path.Substring(0, 1) == "/")
				return this.GetXmlTree().SelectSingleNode(path);

			return this.GetXmlTree().DocumentElement.SelectSingleNode(path);
        }
        //
    }
    //
}
//
